package com.yuanfeng.userms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.LimitUtils;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.userms.dto.AdminRightsGroupDTO;
import com.yuanfeng.userms.entity.UserAdminBaseEntity;
import com.yuanfeng.userms.entity.UserAdminRightsGroupEntity;
import com.yuanfeng.userms.entity.UserSubSiteEntity;
import com.yuanfeng.userms.mapper.UserAdminBaseMapper;
import com.yuanfeng.userms.mapper.UserAdminRightsGroupMapper;
import com.yuanfeng.userms.mapper.UserSubSiteMapper;
import com.yuanfeng.userms.service.UserAdminRightsGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class UserAdminRightsGroupServiceImpl extends ServiceImpl<UserAdminRightsGroupMapper, UserAdminRightsGroupEntity>
    implements UserAdminRightsGroupService{

    @Autowired
    private UserSubSiteMapper userSubSiteMapper;

    @Autowired
    private UserAdminBaseMapper userAdminBaseMapper;
    @Override
    public ResponseResult getStaffInfo(Map<String, String> paraMap) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(paraMap.get("token"));
        Boolean torf = LimitUtils.checkLimit("authority", userInfo);
        if (null != userInfo) {
            Map<String, Object> returnMap = new HashMap<String, Object>();
            List<UserSubSiteEntity> listSubSiteInfo =userSubSiteMapper.selectList(new LambdaQueryWrapper<UserSubSiteEntity>());
            List<UserAdminRightsGroupEntity> listRightSGroup = this.baseMapper.selectList(new LambdaQueryWrapper<UserAdminRightsGroupEntity>());
            returnMap.put("listSubSiteInfo", listSubSiteInfo);
            returnMap.put("listRightSGroup", listRightSGroup);
            return ResponseResult.success(returnMap);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult deleteGroupInfo(AdminRightsGroupDTO queryGroupInfo) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(queryGroupInfo.getToken());
        if (null != userInfo) {
            //判断当前权限组是否使用
            Integer num = userAdminBaseMapper.selectCount(new LambdaQueryWrapper<UserAdminBaseEntity>()
                            .eq(UserAdminBaseEntity::getRightsGroupId,queryGroupInfo.getRightsGroupId()));
            if (num == 0) {
                this.baseMapper.deleteById(queryGroupInfo.getRightsGroupId());
                return ResponseResult.success(BizCodeEnume.DELETE_SUCCESS);
            }
            return ResponseResult.fail(BizCodeEnume.GROUPING_IS_RELATED);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult updateGroupInfo(AdminRightsGroupDTO queryGroupInfo) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(queryGroupInfo.getToken());
        if (null != userInfo) {
            this.baseMapper.updateGroupInfo(queryGroupInfo);
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult queryGroupInfo(AdminRightsGroupDTO queryGroupInfo) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(queryGroupInfo.getToken());
        if (null != userInfo) {
            UserAdminRightsGroupEntity adminRightsGroupEntity = this.baseMapper.selectById(queryGroupInfo.getRightsGroupId());
            return ResponseResult.success(adminRightsGroupEntity);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult queryGroupList(Map<String, Object> map) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(map.get("token").toString());
        if (null != userInfo) {
            List<UserAdminRightsGroupEntity> list = this.baseMapper.selectList(new QueryWrapper<>());
            return ResponseResult.success(list);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult addGroupInfo(AdminRightsGroupDTO queryGroupInfo) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(queryGroupInfo.getToken());
        if (null != userInfo) {
            this.baseMapper.addGroupInfo(queryGroupInfo);
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }
}




